package com.bsj.media.media.self;

import com.bsj.consumerQ.until.ExceptionUntil;
import com.bsj.media.init.dto.NetDto;
import com.bsj.media.media.FlvMediaStream;
import com.bsj.media.media.ParserVideoData;
import com.bsj.power.common.util.Conv;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MySelfFlvMedia extends FlvMediaStream {

    private NetDto netDto;

    public int packetData(byte[] fdBody, byte[] dstBuffer) {
        if (fdBody != null) {
            int packetLen = 0;
            try {
                // 提取FDFD协议数据的数据体，19字节+body+校验1+结束位1
                byte[] body = fdBody;
                if (!isPacketFlvHeader) {
                    if (getVideoType() != 3 && (!ParserVideoData.checkIframe(body))) {
                        // 如果是只有音频的时候不判断这个
                        log.warn("打包头---不是I帧，丢掉---key:");
                        return 0;
                    }
                    int videoRate = 0;
                    if (getVideoType() != 3) {
                        // 算出时间间隔
                        videoRate = ParserVideoData.getVideoFrame(body, 0);
                        if (videoRate > 0) {
                            st_videoRate = 1000 / videoRate;
                        }
                        // 获取视频分辨率
                        int solutionRatio = ParserVideoData.getVideoSolution(body, 0);
                        getSolutionRatio(solutionRatio);
                    } else {
                        log.info("进入PC端对讲数据打包：");
                    }
                    // 打FLV头，h264信息
                    packetLen = PacketFirstFLV(body, dstBuffer);
                    netDto.setMediaSaveInfo(getMediaSaveInfo());
                    firstTimestamps = Conv.getLongNetOrder(body, 5);
                } else {
                    packetLen = PacketSecondFLV(body, dstBuffer, 0);
                }
            } catch (Exception e) {
                log.error("打包数据异常！" + ExceptionUntil.getErrorStr(e));
            }
            return packetLen;
        }
        return 0;
    }

    public MySelfFlvMedia(String videoType, int codeType) {
        super(videoType, codeType);
    }

    public void initNetDto(NetDto now) {
        this.netDto = now;
    }
}
